# 文件名: seleniums
# 当前用户: shiping.zheng
# 当前日期: 2024/9/13
# 当前时间: 10:17
# 项目名称: Flag
"""
方式：使用redis查询图片验证码
    1.登录页面 2，通过接口查询到uuid 3.通过uuid查询redis获取验证码
"""
import requests
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import io
import base64
from PIL import Image
import pytesseract
import cv2
import numpy as np
import redis
from rediscluster import RedisCluster
from common import request_Util
from common import yaml_util

# 设置EdgeDriver路径
edge_driver_path = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe'
edge_options = Options()
service = Service(edge_driver_path)
driver = webdriver.Edge(service=service, options=edge_options)
driver.get('https://xuntian-test2.tclpv.com/getechLogin')
time.sleep(2)
# 使用 XPath 定位输入框
input_element = driver.find_element(By.XPATH,"//input[@placeholder='请输入用户名']")
# 在输入框中输入文本 "admin"
input_element.send_keys("admin")
time.sleep(1)
input_element = driver.find_element(By.XPATH,"//input[@type='password']")
input_element.send_keys("tcl123")
time.sleep(1)

#调用接口获取验证码的uuid
res = requests.get(url='https://xuntian-test2.tclpv.com/api/auth/verify/getCode?t=1733818299414')
uuid = res.json()['data']['uuid']

#使用redis通过uuid获取验证码
# 初始化 Redis 集群连接
startup_nodes = [{"host": "10.51.2.30", "port": "6379"}]  # 使用集群中的一个节点进行初始化
try:
    # 创建 RedisCluster 实例
    rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
    key = 'auth:user:code:'+uuid
    # 示例操作：获取键的值
    # key = 'auth:user:code:93941fcea2d64de5ad6c790c75166c5a'
    value = rc.get(key)

    if value is not None:
        print(f"键 '{key}' 的值是: {value}")
    else:
        print(f"键 '{key}' 不存在或没有值")

except Exception as e:
    print(f"连接到 Redis 集群时出错: {e}")

input_element = driver.find_element(By.XPATH,"//input[@placeholder='请输入左图结果']")
input_element.send_keys(value)
login_button = driver.find_element(By.XPATH, "//button[span[text()='登录']]")
login_button.click()
input("Press Enter to close the browser...")
